package com.taptech.doufu.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;

import com.taptech.doufu.ui.adapter.TTSearchContentAdapter;
import com.taptech.doufu.bean.HomeTopBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.StartActivityUtil;

public class HomeSearchContentListView extends PullToRefreshListView
{
    private float xDistance, yDistance, xLast, yLast;
    private TTSearchContentAdapter mAdapter;
    private Context mContext;

    public HomeSearchContentListView(Context context)
    {
        super(context);
        init(context);
    }

    public HomeSearchContentListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    public HomeSearchContentListView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

    }

    private void init(Context context)
    {
        mContext = context;

        this.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, final int position, long arg3)
            {
                try
                {
                    HomeTopBean bean = mAdapter.getBean(position - 1);
                    if (bean != null)
                    {
                        if (DiaobaoUtil.String2Int(bean.getTopic_type()) == Constant.OBJECT_TYPE_NOVEL)
                        {
                            StartActivityUtil.novelOnclick(getContext(), bean.getId());
                        } else
                        {
                            DiaobaoUtil.contentClick(mContext, bean);
                        }
                    }

                } catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
        });
    }

    public void setAdapter(TTSearchContentAdapter adapter)
    {
        mAdapter = (TTSearchContentAdapter) adapter;
        super.setAdapter(adapter);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        switch (ev.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;

                if (xDistance > yDistance)
                {
                    return false;
                }
        }

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }

        return super.onTouchEvent(event);
    }


}
